# -*- coding:utf-8 -*-
import urllib
import simplejson
import re


def getSongInfobyId(id):
    """
    theurl = "http://www.xiami.com/song/{{id}}"
    """
    dic ={}
    theurl = "http://www.xiami.com/song/"+str(id)
    thestr = urllib.urlopen(theurl).read()

    # music name here
    titleregex = "id=\"title(\s|\S)*?</div>"
    trm = re.search(titleregex,thestr)
    if trm:
        titlestr = thestr[trm.start():trm.end()]
        b = titlestr.index("h1")
        e = titlestr.index("h1",b+3)
        name =  titlestr[b+3:e-2]
        dic['name'] = name
        #print name
    else:
        raise Exception("title not found")


    #albuminfo,not all the info are there
    albuminforegex = "albums_info(\s|\S)*?</table>"
    arm = re.search(albuminforegex,thestr)
    if arm:
        albuminfostr = thestr[arm.start():arm.end()]
        #print albuminfostr
    else:
        raise Exception("album_info not found")

    ##album
    albumregex = "album/(\s|\S)*?</a>"
    albumm = re.search(albumregex,albuminfostr)
    if albumm:
        tmpstr = albuminfostr[albumm.start():albumm.end()]
        tmpdic = {}
        tmpdic['albumid'] = int(tmpstr[6:tmpstr.index("\"")])
        tmpdic['albumname'] = tmpstr[tmpstr.index(">")+1:tmpstr.index("<")]
        dic['album'] = tmpdic
    
    ##artist
    artistregex = "artist/(\s|\S)*?</a>"
    artistm = re.search(artistregex,albuminfostr)
    if artistm:
        tmpstr = albuminfostr[artistm.start():artistm.end()]
        tmpdic = {}
        tmpdic['artistid'] = int(tmpstr[7:tmpstr.index('\"')])
        tmpdic['artistname'] = tmpstr[tmpstr.index(">")+1:tmpstr.index("<")]
        dic['artist'] = tmpdic
    
    trregex = "tr(\s|\S)*?</tr>"
    for trm in re.finditer(trregex,albuminfostr):
        tmpstr = albuminfostr[trm.start():trm.end()]
        if tmpstr.count("作词") != 0:
            ##lyricist
            #print tmpstr
            dic['lyricistname'] = tmpstr[tmpstr.rindex("\">")+2:tmpstr.rindex("</td")]
            continue
        if tmpstr.count("作曲") != 0:
            ##composer
            dic['composername'] = tmpstr[tmpstr.rindex("\">")+2:tmpstr.rindex("</td")]
            continue

    #the lyric
    ##div class lrc_main
    
    #comment


    return dic


def parserSongHtml(id):
    return None


#相关图片
#<img class="cdCDcover185" src="http://img.xiami.com/./images/album/img60/1260/33354_2.jpg">
def getCoverPicLink(songid):
    s = getHtmlSource(songid)
    imgregex = ur"cdCDcover185(\s|\S)*?>"
    imgsearch = re.search(imgregex,s)
    if imgsearch:
        imgstr = s[imgsearch.start():imgsearch.end()]
    else:
        f = open("songpagetmp.txt","w")
        print>>f,s
        f.close()
        raise Exception("song page format error")
    #print imgstr
    link = imgstr[imgstr.index("src")+5:imgstr.rindex("\"")]
    return link

def getHtmlSource(songid):
    theurl = "http://www.xiami.com/song/"+str(songid)
    l = urllib.urlopen(theurl)
    s = l.read()
    l.close()
    return s

if __name__ == "__main__":
    #print getCoverPicLink("1770224315")
    #tmpdic =  getSongInfobyId("1770620373")
    #print tmpdic['album']['albumname']
    #print tmpdic['artist']['artistname']
    tmpdic =  getSongInfobyId("3509276")
    print tmpdic
    #print tmpdic['album']
